package org.lifl.compozer;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class Container implements Component {
	@Override
	public ComponentMachine getMachine() { return this.machine; }
	protected final ComponentMachine machine;
	
	protected List<Connector<?>> connectors;
	
	public Container(ComponentMachine machine) throws NotSameComponentMachineException {
		this.connectors = new ArrayList<Connector<?>>();
		this.machine = machine;
		this.machine.attach(this);
	}
	
	@Override
	public Collection<Connector<?>> getAllConnectors() {
		return new ArrayList<Connector<?>>(this.connectors);
	}
	
	public void addComponent(Component c) 
			throws NotSameComponentMachineException,
			MultipleCompositionException {
		this.machine.addComponentIntoContainer(this, c);
	}
	
	public boolean tryAddComponent(Component c) {
		try {
			this.machine.addComponentIntoContainer(this, c);
		} catch (Exception e) { return false; }
		return true;
	}
	
	public boolean contains(Component c) {
		return this.machine.composition.get(this).contains(c);
	}
}
